package com.app.backend.common.response;

import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.Getter;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;

import java.io.Serializable;
import java.time.LocalDateTime;

/**
 * @program: BaseResponse
 * @description: BaseResponse
 * @author: Zhun.Xiao
 * @create: 2018-10-29 14:20
 **/

@Data
@Builder
@AllArgsConstructor
public class BaseResponse<T> implements Serializable {


    private static final ResponseCode SUCCESS = ResponseCode.SUCCESS;

    @ApiModelProperty(value = "返回码")
    private StatusCode code;
    @ApiModelProperty(value = "返回消息")
    private String msg;
    @ApiModelProperty(value = "返回对象")
    private T data;
    @ApiModelProperty("响应时间")
    @JSONField(format = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime time;


    public BaseResponse() {
        this.setCode(SUCCESS);
        this.msg = SUCCESS.msg();
    }

    public BaseResponse(T data) {
        this();
        this.data = data;
    }

    public BaseResponse(ResponseCode code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public void setCode(ResponseCode code) {
        this.code = code;
        this.msg = code.getMsg();
    }

    public static BaseResponse<Boolean> result(ResponseCode responseCode) {
        return result(responseCode, null);
    }

    public static <T> BaseResponse<T> result(ResponseCode responseCode, T data) {
        return result(responseCode, null, data);
    }

    public static <T> BaseResponse<T> result(ResponseCode responseCode, String message, T data) {
        String apiMessage = responseCode.getMsg();
        if (StringUtils.isBlank(message)) {
            message = apiMessage;
        }
        @SuppressWarnings("unchecked")
        BaseResponse<T> response = (BaseResponse<T>) BaseResponse.builder()
                .code(responseCode)
                .msg(message)
                .data(data)
                .time(LocalDateTime.now())
                .build();
        return response;
    }

    public static BaseResponse<Boolean> success() {
        return success(null);
    }

    public static <T> BaseResponse<T> success(T data) {
        return result(ResponseCode.SUCCESS, data);
    }

    public static <T> BaseResponse<T> success(String message, T data) {
        return result(ResponseCode.SUCCESS, message, data);
    }

    public static BaseResponse<Boolean> fail() {
        return fail(ResponseCode.SYSTEM_ERROR);
    }

    public static BaseResponse<Boolean> fail(ResponseCode responseCode) {
        return result(responseCode, null);
    }

    public static BaseResponse<String> fail(String message) {
        return result(ResponseCode.SYSTEM_ERROR, message, null);
    }

    public static <T> BaseResponse<T> fail(ResponseCode responseCode, String message) {
        return result(responseCode, message, null);
    }

    public static <T> BaseResponse<T> fail(String code, String message) {
        return result(ResponseCode.getByCode(code), message, null);
    }

    @Override
    public String toString() {
        return new ToStringBuilder(this)
                .append("code", code.code())
                .append("msg", msg)
                .append("data", data)
                .toString();
    }

}
